package com.ybt.configration;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ruleengine.endpoint.data.factory.IEndpointDataFactory;
import com.ruleengine.frame.interceptor.IRuleEngineInterceptor;

public class YBTRuntimeConfiguration {
    private static Logger logger = Logger.getLogger(YBTRuntimeConfiguration.class);
    private static YBTRuntimeConfiguration rconfig;
    private volatile static YBTConfigration config;
    private int excuteTimeout;
    private long messageTimeout;
    private Charset fileCharset;
    private int excutePoolSize;
    
    private YBTRuntimeConfiguration(){
        config = YBTConfigration.getInstance();
        excuteTimeout = Integer.valueOf(config.getProperty(IYBTConfigration.EXECUTOR_TIMEOUT));
        messageTimeout = Long.valueOf(config.getProperty(IYBTConfigration.SEND_MESSAGE_TIME_OUT));
        fileCharset = Charset.forName(config.getProperty(IYBTConfigration.CONFIG_FILE_CHARSET));
        excutePoolSize = Integer.valueOf(config.getProperty(IYBTConfigration.EXECUTOR_POOL_SIZE));
    }
    
    public static YBTRuntimeConfiguration getInstance(){
        if( rconfig == null){
            synchronized(YBTRuntimeConfiguration.class){
                if( rconfig == null){
                    rconfig = new YBTRuntimeConfiguration();
                }
            }
        }
        return rconfig;
    }
    
    public int getExcuteTimeout(){
        return excuteTimeout;
    }
    
    public long getMessageTimeout(){
        return messageTimeout;
    }

    public Charset getConfigFileCharset(){
        return fileCharset;
    }
    
    public String getProperty(String key){
        return config.getProperty(key);
    }

    public int getExcutePoolSize() {
        return excutePoolSize;
    }
    
    
    public IEndpointDataFactory getEndpointDataFactory(){
        String impl = config.getProperty(IYBTConfigration.ENDPOINT_DATA_FACTORY);
        if(impl==null){
            throw new RuntimeException("the parameter "+IYBTConfigration.ENDPOINT_DATA_FACTORY+ "is not exsit");
        }
        try{
            Class c = Class.forName(impl);
            return (IEndpointDataFactory)c.newInstance();
        }catch( Exception e){
            throw new RuntimeException("instance object ["+impl+ "] error",e);
        }
    }

    public List<IRuleEngineInterceptor> getRuleEngineInterceptors(){
        String impls = config.getProperty(IYBTConfigration.WORKFLOW_PRE_INTERCEPTOR);
        List<IRuleEngineInterceptor> interArray = new ArrayList<IRuleEngineInterceptor>();
        if(impls==null){
            logger.debug("pre interceptors for workflow is not exsit");
        }
        try{
            String[] inters = impls.split(",");
            for(String strClass : inters){
                Class c = Class.forName(strClass);
                interArray.add((IRuleEngineInterceptor)c.newInstance());
            }
            return  interArray;
        }catch( Exception e){
            throw new RuntimeException("instance interceptors error",e);
        }
    }

}
